package dao.impl;

import dao.BaseDAO;
import dao.UserDAO;
import pojo.UserDO;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserDAOImpl extends BaseDAO implements UserDAO {
    @Override
    public UserDO getByUsernameAndPsw(String username, String psw) {
        this.getConnection();
        statement = null;
        rs = null;
        UserDO user = null;
        String sql = "select * from t_user where u_name=? and u_psw=?";
        try {
            statement = con.prepareStatement(sql);
            statement.setString(1, username);
            statement.setObject(2, psw);
            rs = statement.executeQuery();
            if (rs.next()) {
                user = new UserDO();
                user.setId(rs.getInt("u_id"));
                user.setUsername(rs.getString("u_name"));
                user.setPassword(rs.getString("u_psw"));
                user.setMoney(rs.getDouble("u_money"));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close(con, statement, rs);
        }
        return user;
    }


    @Override
    public void update(UserDO user) {
            // 1. 创建连接
            this.getConnection();
            String sql = "update t_user set u_psw=?,u_money=? where u_id=?";
            try {
                // 2. 获取执行SQL语句对象
                statement = con.prepareStatement(sql);
                statement.setObject(1, user.getPassword());
                statement.setObject(2, user.getMoney());
                statement.setObject(3, user.getId());

                // 3. 执行SQL语句
                int row = statement.executeUpdate();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            } finally {
                // 4. 关闭连接
                close(con, statement);
            }
        }

    public UserDO getById(int id) {
         con = getConnection();
         statement = null;
         rs = null;
        UserDO user = null;
        String sql = "select * from t_user where u_id=?";
        try {
            statement = con.prepareStatement(sql);
            statement.setObject(1, id);
            rs = statement.executeQuery();
            if (rs.next()) {
                user = new UserDO();
                user.setId(rs.getInt("u_id"));
                user.setUsername(rs.getString("u_name"));
                user.setPassword(rs.getString("u_psw"));
                user.setMoney(rs.getDouble("u_money"));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close(con, statement, rs);
        }
        return user;
    }

    @Override
    public UserDO getByUsername(String username) {
        Connection con = getConnection();
        PreparedStatement statement = null;
        ResultSet rs = null;
        UserDO user = null;
        String sql = "select * from t_user where u_name=?";
        try {
            statement = con.prepareStatement(sql);
            statement.setObject(1, username);
            rs = statement.executeQuery();
            if (rs.next()) {
                user = new UserDO();
                user.setId(rs.getInt("u_id"));
                user.setUsername(rs.getString("u_name"));
                user.setPassword(rs.getString("u_psw"));
                user.setMoney(rs.getDouble("u_money"));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close(con, statement, rs);
        }
        return user;
    }


}
